package com.music;

import android.app.Service;
import android.content.Intent;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnCompletionListener;
import android.media.MediaPlayer.OnErrorListener;
import android.media.MediaPlayer.OnPreparedListener;
import android.os.IBinder;
import android.os.PowerManager;
import android.util.Log;

public class MusicService extends Service implements OnCompletionListener,
		OnPreparedListener, OnErrorListener {

	final static String TAG = "Music Application";

	public static final String ACTION_PLAY = "com.music.action.PLAY";
	public static final String ACTION_PAUSE = "com.music.action.PAUSE";
	public static final String ACTION_STOP = "com.music.action.STOP";
	public static final String ACTION_INITIALPLAY = "com.music.action.INITIALPLAY";

	// MediaPlayer mPlayer = null;
	private MediaPlayer mMediaPlayer2;

	/**
	 * used by MediaPlayer to find current position of played file
	 */
	private int length;
	/**
	 * keep resource number generated by R file
	 */
	private int songNumber = -1;

	enum State {

		Stopped, Preparing, Playing, Paused
	};

	State mState = State.Preparing;

	// boolean mStartPlayingAfterPreparing = false;

	public void onCreate() {
		Log.i(TAG, "Service on create");
	}

	@Override
	public int onStartCommand(Intent intent, int flags, int startId) {
		String action = intent.getAction();
		if (action.equals(ACTION_PLAY)) {
			processPlayRequest();
			System.out.println("MusicService.onStartCommand() is it working?");
		} else if (action.equals(ACTION_PAUSE))
			processPauseRequest();
		else if (action.equals(ACTION_STOP))
			processStopRequest();
		else if (action.equals(ACTION_INITIALPLAY)) {
			songNumber = intent.getIntExtra("musicFileName", -1);
			System.err.println("SongNumber is " + songNumber);
			processInitialPlay(songNumber);
		}

		return START_NOT_STICKY;

	}

	void processInitialPlay(int resourceNumber) {
		if (resourceNumber == -1) {
			System.err.println("Ressource not given");
		} else {
			mMediaPlayer2 = MediaPlayer.create(this, resourceNumber);
			System.out.println("MusicService.processInitialPlay()");
			mMediaPlayer2.start();
		}
	}

	void processPlayRequest() {
		System.err.println("Play request is on and the state is " + mState);
		if (mMediaPlayer2 == null && songNumber != -1) {
			mMediaPlayer2 = MediaPlayer.create(this, songNumber);
		}

		if (mState == State.Paused) {
			mMediaPlayer2.seekTo(length);
			mMediaPlayer2.start();
			mState = State.Playing;
		} else {
			mMediaPlayer2.start();
			mState = State.Playing;
		}

	}

	void processPauseRequest() {
		System.err.println("Pause request is on and the state is " + mState);
		if (mState == State.Playing) {
			mMediaPlayer2.pause();
			relaxResources(false);
			length = mMediaPlayer2.getCurrentPosition();
			mState = State.Paused;
		}

	}

	void processStopRequest() {
		System.err.println("Stop request is on and the state is " + mState);
		if (mState == State.Playing) {
			mMediaPlayer2.stop();
			mState = State.Stopped;
		}
	}

	void relaxResources(boolean releaseMediaPlayer) {

		if (releaseMediaPlayer && mMediaPlayer2 != null) {
			mMediaPlayer2.reset();
			mMediaPlayer2.release();
			mMediaPlayer2 = null;
		}

	}

	/**
	 * TODO
	 */

	@Override
	public void onDestroy() {
		// Service is being killed, so make sure we release our resources
		// mState = State.Stopped;
		// relaxResources(true);
		// giveUpAudioFocus();
	}

	@Override
	public IBinder onBind(Intent arg0) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public boolean onError(MediaPlayer arg0, int arg1, int arg2) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public void onPrepared(MediaPlayer arg0) {
		// TODO Auto-generated method stub

	}

	@Override
	public void onCompletion(MediaPlayer arg0) {
		// TODO Auto-generated method stub

	}

}
